package co.salehi.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import co.salehi.domain.Cart;
import co.salehi.domain.Ring;

@RooWebScaffold(path = "rings", formBackingObject = Ring.class)
@RequestMapping("/rings")
@Controller
public class RingController {

	@RequestMapping(params = "addtocart", method = RequestMethod.POST)
    public String addToCart(HttpServletRequest httpServletRequest) {
        return "redirect:/sells?showCart";
    }
	
	@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model uiModel) {
		uiModel.addAttribute("ring", Ring.findRing(id));
		addDateTimeFormatPatterns(uiModel);
		return "rings/update";
	}
	
	@RequestMapping(value = "/{id}", params = "addtocartform", method = RequestMethod.GET)
	public String addToCartForm(@PathVariable("id") Long id, Model uiModel) {
		uiModel.addAttribute("ring", Ring.findRing(id));
		addDateTimeFormatPatterns(uiModel);
		return "rings/addtocart";
	}


	@PreAuthorize("hasAuthority('ROLE_ADMIN')")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Long id,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size,
			Model uiModel) {
		Ring.findRing(id).remove();
		uiModel.asMap().clear();
		uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
		uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
		return "redirect:/rings";
	}

	@PreAuthorize("hasAuthority('ROLE_ADMIN')")
	@RequestMapping(method = RequestMethod.GET, params = { "soldouts" })
	public String listSolds(
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size,
			Model uiModel) {
		if (page != null || size != null) {
			int sizeNo = size == null ? 10 : size.intValue();
			uiModel.addAttribute("rings", Ring.findSoldRingEntries(
					page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));
			float nrOfPages = (float) Ring.countSoldRings() / sizeNo;
			uiModel.addAttribute(
					"maxPages",
					(int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1
							: nrOfPages));
		} else {
			uiModel.addAttribute("rings", Ring.findAllSoldRings());
		}
		addDateTimeFormatPatterns(uiModel);
		return "rings/listSolds";
	}

	@PreAuthorize("hasAuthority('ROLE_ADMIN')")
	@RequestMapping(params = { "unsell", "selectedItems" }, method = RequestMethod.GET)
	public String unsell(Model uiModel, HttpServletRequest request) {

		String str = request.getParameter("selectedItems");

		for (String idStr : str.split(",")) {
			Ring ring = Ring.findRing(Long.parseLong(idStr));
			ring.setSellDate(null);
			ring.merge();
		}

		uiModel.addAttribute("soldouts", true);
		return "rings/listSolds";
	}

}
